﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ListViewItem
{
    public partial class Form1 : Form
    {
        private object mouseClickItem;

        public Form1()
        {
            InitializeComponent();
            InitListView();
          
        }

        private void InitListView()
        {
            // 设置显示模式
            listView1.View = View.Details;
            // 整行选中
             listView1.FullRowSelect = true;

            listView1.LabelEdit = true;

            // 设置列名
            // 宽度值-2表示自动调整宽度
            listView1.Columns.Add("姓名", 150, HorizontalAlignment.Left);
            listView1.Columns.Add("学号", 150, HorizontalAlignment.Left);
            listView1.Columns.Add("手机号", -2, HorizontalAlignment.Left);


           
        }
        //private void buttonShowInput_Click(object sender, EventArgs e)
        //{
        //    using (InputForm inputForm = new InputForm())
        //    {
        //        if (inputForm.ShowDialog() == DialogResult.OK)
        //        {
        //            string userInput = inputForm.InputValue;
        //            MessageBox.Show("用户输入: " + userInput);
        //        }
        //    }
        //}


        // 点击“重命名”菜单
        //private void menu_Rename_Click(object sender, EventArgs e)
        //{
        //    this.mouseClickItem.BeginEdit();
        //}

        // 鼠标右键点击，弹出菜单
        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Right)
            {
                System.Windows.Forms.ListViewItem item = listView1.GetItemAt(e.X, e.Y);
                this.mouseClickItem = item;

                //menu_Rename.Enabled = (item != null);

                contextMenuStrip1.Show(listView1, e.Location);
            }
        }

        // 结束列表项的编辑
        private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e)
        {
            int index = e.Item; // 当前项的索引
            string label = e.Label; // 新输入的文本

            // 检查是否冲突
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (index == i) continue;
                if (label == listView1.Items[i].Text)
                {
                    e.CancelEdit = true;
                    MessageBox.Show("名字重复", "提示");
                    return;
                }
            }

            e.CancelEdit = false; // 接受此输入


        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void add(object sender, EventArgs e)
        {
            //InputForm是InputForm.cs文件的，实例化InputForm对象
            InputForm inputForm = new InputForm();
            // 提示用户输入数据
            string name = Input("请输入姓名：");
            string studentId = Input("请输入学号：");
            string phone = Input("请输入手机号：");

            // 创建一个新的 ListViewItem
            System.Windows.Forms.ListViewItem item = new System.Windows.Forms.ListViewItem(name);
            item.SubItems.Add(studentId);
            item.SubItems.Add(phone);

            // 将 ListViewItem 添加到 ListView 中
            listView1.Items.Add(item);
      
            MessageBox.Show("add sucess");


        }

        private string Input(string prompt)
        {
            //DialogResult result = MessageBox.Show(prompt, "输入", MessageBoxButtons.OKCancel);
            //if (result == DialogResult.OK)
            //{

            //    return prompt; // 这里返回的是提示文本，实际应用中应使用其他方法获取用户输入
            //}
            //return string.Empty;



            //using 语句创建了一个作用域
            //在这个作用域内，inputForm 是有效的。当离开这个作用域时（例如，当 using 块执行完毕后），inputForm 将被自动释放
            using (InputForm inputForm = new InputForm())
            {
             
                inputForm.Text = prompt;
                if (inputForm.ShowDialog() == DialogResult.OK)
                {
                    return inputForm.InputValue;
                }
            }
            return string.Empty;
        }
        private void delete(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                DialogResult result = MessageBox.Show("确定要删除选中的项吗？", "确认删除", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    foreach (System.Windows.Forms.ListViewItem selectedItem in listView1.SelectedItems)
                    {
                        listView1.Items.Remove(selectedItem);
                        MessageBox.Show("delete sucess");
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的项", "提示");
            }

         
        }
    }
}
